import constant from './constant'

/**
 * 根据环境区分域名
 * @param path
 * @returns {string}
 */
export function envDistinguishDomain (path = '') {
  let domain = process.env.NODE_ENV === 'development' ?
    process.env.VUE_APP_API_BASE_URL :
    document.location.origin + process.env.VUE_APP_API_BASE_URL
  return domain + path
}

/**
 * 常量状态格式化
 * @param status 值
 * @param type  常量类型
 * @param color  是否带色彩
 * @param defaultVal  默认值
 * @returns {string|*}
 */
export function constantFormatHtml (status = '', type = '', color = '', defaultVal = '') {
  if (status !== '' && type !== '') {
    let value = constant[type][status] ? constant[type][status] : false
    if (color && constant['Color' + type]) {
      return value ? `<i class="fs-normal" style="color: ${constant['Color' + type][status]}">${value}</i>` : defaultVal
    }
    return value ? value : defaultVal
  }
  return ''
}

/**
 * 获取下拉框数据
 * @param type 常量类型
 * @param isAll 是否包含全部  用于全部得值
 * @param filter 过滤得值
 * @param valuePrefix 值前缀
 * @returns {[{label: string, value: string}]|[]}
 */
export function downConstantList (type = '', isAll = '', filter = [], valuePrefix = false) {
  let result = isAll !== false ? [{ label: '全部', value: isAll }] : []
  if (constant[type]) {
    let value = constant[type]
    for (let key in value) {
      if (filter.length === 0 || !filter.includes(Number(key))) {
        let val = valuePrefix ? valuePrefix + key : Number(key)
        result.push({ label: value[key], value: val })
      }
    }
  }
  return result
}
